package display;

import java.awt.Canvas;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferStrategy;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;

import entity.misc.Entrance;
import entity.misc.Exit;
import entity.terrain.Terrain;

import level.*;

public class Game extends Canvas implements MouseListener{
	private int mouseXco;
	private int mouseYco;
	
	public static final int WIDTH = 320;

	public static final int HEIGHT = 230;

	public BufferStrategy buffStrat;

	/* Environment variables */
	Level level;
	Manager lemmings;
	Terrain terrain;
	Entrance entrance;
	Exit exit;

	public Game() {
		JFrame frame = new JFrame("Lemmings");
		
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout(1,1));
		panel.setBackground(Color.white);
		frame.getContentPane().add(panel);
		
		setBounds(0, 0, WIDTH, HEIGHT);
		panel.setPreferredSize(new Dimension(200, 300));
		panel.add(BorderLayout.CENTER, this);
		
		// hack of a way to add butttons, gotta update but buttons will work!!!
		//-----------------------------------
		JPanel menu = new JPanel();
		menu.setLayout(new GridLayout(1,8));
		JButton button = new JButton("0");
		panel.add(BorderLayout.SOUTH, button);
		JButton button1 = new JButton("1");
		menu.add(button);
		menu.add(button1);
		panel.add(BorderLayout.SOUTH, menu);
		//----------------------------------
		
		frame.setBounds(50, 50, WIDTH, HEIGHT);
		frame.setVisible(true);
		frame.addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		// Register for mouse events on frame.
		frame.addMouseListener(this);
        addMouseListener(this);
        createBufferStrategy(2);
        buffStrat = getBufferStrategy();

	}

	public void init() {
		/*
		 * terrain = new Terrain[2]; terrain[0] = new
		 * Terrain("images/terrain/ground.gif", 1, false, false); terrain[1] =
		 * new Terrain("images/terrain/obstacle.gif", 1, false, false); level =
		 * new Level(40, 180, 300, 200, 200, 1 ,terrain); lemmings = new
		 * Manager(1, level);
		 */
		terrain = new Terrain("resource/graphics/levels/One/level_1.gif",
				1, false, false);
		entrance = new Entrance( 
				"resource/graphics/levels/One/entrance_1.gif",12,38);
		exit = new Exit(
				"resource/graphics/levels/One/exit_1.gif",149, 102);
		//level = new Level(34, 40, 163, 121, 200, 1, terrain, entrance, exit);
		level = new Level(34, 40, 162, 119, 200, 1, terrain, entrance, exit);
		lemmings = new Manager(1, level);
	}

	public void logic() {
		lemmings.play();
	}
	
	public void paintWorld(){
		Graphics g = buffStrat.getDrawGraphics();
		g.setColor(Color.black);
		g.fillRect(0,0,getWidth(),getHeight());
		try {
			lemmings.draw(g);
		} catch (NullPointerException e){
			
		}
		buffStrat.show();
	}

	public void run() {
		init();
		while (!lemmings.gameOver()) {
			logic();
			paintWorld();
			try {
				Thread.sleep(50);
			} catch (InterruptedException e) {
			}
		}
		System.out.println("GAMEOVER!!!");
	}

	public static void main(String[] args) {
		Game game = new Game();
		game.run();
	}

	public void mouseDragged(MouseEvent e) {
		// do nothing
	}

    public void mousePressed(MouseEvent e) {
     
     }

     public void mouseReleased(MouseEvent e) {
      
     }

     public void mouseEntered(MouseEvent e) {
      
     }

     public void mouseExited(MouseEvent e) {
      
     }

     public void mouseClicked(MouseEvent e) {
    	 mouseXco = e.getX();
    	 mouseYco = e.getY();
    	 System.out.println("x co: "+mouseXco+" y co: "+mouseYco);
    	 lemmings.setMouseCoordinates(mouseXco, mouseYco);
     }

  


}